/*      $Id: date.c,v 1.4 2006/03/15 04:13:58 jfb Exp $    */
/*
 * Copyright (c) 2003 Jean-Francois Brousseau <phrakt@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>
#include <sys/time.h>

#include <time.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

#include <canopy/log.h>

#include "http.h"
#include "date.h"
#include "private.h"


static const char *weekdays[] = {
	"Sunday",
	"Monday",
	"Tuesday",
	"Wednesday",
	"Thursday",
	"Friday",
	"Saturday"
};


static const char *months[] = {
	"January",
	"February",
	"March",
	"April",
	"May",
	"June",
	"July",
	"August",
	"September",
	"October",
	"November",
	"December"
};



/*
 * http_date_format()
 *
 * Format the date represented in seconds since the Epoch in <date> to a
 * string representation.  The exact format is specified in <fmt> and can be
 * any of the following formats:
 *
 *	-RFC 1123 (preferred):	Sun, 06 Nov 1994 08:49:37 GMT
 *	-RFC 850 (obsolete):	Sunday, 06-Nov-94 08:49:37 GMT
 *	-ANSI C ctime():	Sun Nov  6 08:49:37 1994
 *	-ISO 8601:		1994-11-06T08:49:37
 *
 * Returns 0 on success, or -1 on failure.
 */

int
http_date_format(time_t date, int format, char *dst, size_t dlen)
{
	struct tm res;

	switch (format) {
	case HTTP_DATE_RFC1123:
		gmtime_r(&date, &res);
		snprintf(dst, dlen, "%3s, %02u %3s %4u %02u:%02u:%02u GMT",
		    weekdays[res.tm_wday], res.tm_mday, months[res.tm_mon],
		    res.tm_year + 1900, res.tm_hour, res.tm_min, res.tm_sec);
		break;

	case HTTP_DATE_RFC850:
		break;

	case HTTP_DATE_ANSIC:
		if (dlen < 26) {
			http_log(CNP_LOG_ERR,
			    "buffer too small for ANSI C time format");
			return (-1);
		}
		if (ctime_r(&date, dst) == NULL) {
			http_log(CNP_LOG_ERRNO, "failed to format time");
			return (-1);
		}

		break;

	case HTTP_DATE_ISO8601:
		gmtime_r(&date, &res);
		snprintf(dst, dlen, "%4d-%02d-%02dT%02u:%02u:%02u",
		    res.tm_year + 1900, res.tm_mon + 1, res.tm_mday,
		    res.tm_hour, res.tm_min, res.tm_sec);
		break;

	default:
		http_log(CNP_LOG_ERR, "invalid date format %u", format);
		return (-1);
	}

	return (0);
}
